package com.miracle.mango.entity.permission;

import com.miracle.mango.entity.permission.Permission;
import com.miracle.mango.entity.permission.PermissionMapper;
import com.miracle.mango.util.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import java.util.UUID;

/**
 * @description:
 * @author: wangxf
 * @date: Created in 2020/8/22 23:33
 */
@Service
@Validated
public class PermissionServive {

    @Autowired
    private PermissionMapper permissionMapper;

    public int save(@Valid Permission permission) {
        if(StringUtils.isEmpty(permission.getId())) {
            permission.setId(UUID.randomUUID().toString());
        }
        return permissionMapper.save(permission);
    }

    public int update(Permission permission) {
        Permission saved = permissionMapper.getById(permission.getId());
        if (null == saved) {
            throw new ServiceException("操作的权限不存在!");
        }
        BeanUtils.copyProperties(permission, saved);
        return permissionMapper.update(permission);
    }

    public int removeById(String id) {
        return permissionMapper.removeById(id);
    }

}
